package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.dto.ImportDTO;
import com.ruoyi.system.domain.dto.RechargeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RechargeMapper;
import com.ruoyi.system.domain.Recharge;
import com.ruoyi.system.service.IRechargeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 充值记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-10-24
 */
@Service
public class RechargeServiceImpl implements IRechargeService 
{
    @Autowired
    private RechargeMapper rechargeMapper;

    /**
     * 查询充值记录
     * 
     * @param id 充值记录ID
     * @return 充值记录
     */
    @Override
    public Recharge selectRechargeById(Long id)
    {
        return rechargeMapper.selectRechargeById(id);
    }

    /**
     * 查询充值记录列表
     * 
     * @param recharge 充值记录
     * @return 充值记录
     */
    @Override
    public List<Recharge> selectRechargeList(Recharge recharge)
    {
        return rechargeMapper.selectRechargeList(recharge);
    }

    /**
     * 新增充值记录
     * 
     * @param recharge 充值记录
     * @return 结果
     */
    @Override
    public int insertRecharge(Recharge recharge)
    {
        recharge.setCreateTime(DateUtils.getNowDate());
        return rechargeMapper.insertRecharge(recharge);
    }

    /**
     * 修改充值记录
     * 
     * @param recharge 充值记录
     * @return 结果
     */
    @Override
    public int updateRecharge(Recharge recharge)
    {
        return rechargeMapper.updateRecharge(recharge);
    }

    /**
     * 删除充值记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteRechargeByIds(String ids)
    {
        return rechargeMapper.deleteRechargeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除充值记录信息
     * 
     * @param id 充值记录ID
     * @return 结果
     */
    @Override
    public int deleteRechargeById(Long id)
    {
        return rechargeMapper.deleteRechargeById(id);
    }
    /**
     *  充值排名
     */
    @Override
    public List<RechargeDTO> rechargeList(Long shopId) {

        List<RechargeDTO> list = rechargeMapper.selectList(shopId);

        return list;
    }

    /**
     * web info
     */

    @Override
    public List<RechargeDTO> totalList(Long shopId) {

        List<RechargeDTO> list = rechargeMapper.totalList(shopId);

        return list;
    }
    /**
     *  总金额
     */
    @Override
    public BigDecimal totalMoney(Long shopId) {

        BigDecimal bigDecimal = rechargeMapper.totalMoney(shopId);

        return bigDecimal;
    }
    /**
     * 导入
     */
    @Override
    public String importRecharge(List<ImportDTO> rechargeList) {

        if (StringUtils.isNull(rechargeList) || rechargeList.size() == 0) {
            throw new BusinessException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        String msg = null;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ImportDTO importDTO : rechargeList) {
            try {
                // 验证是否存在这个用户
                boolean userFlag = false;

                if (StringUtils.isBlank(importDTO.getName())){

                    userFlag = false;
                    msg = "会员名称为空！";
                    failureNum++;
                    failureMsg.append(failureNum + ". "+ msg);
                    break;
                }

                if (importDTO.getActivityId() == null){

                    userFlag = false;
                    msg = "活动期数为空！";
                    failureNum++;
                    failureMsg.append(failureNum + ". " + importDTO.getName() + msg);
                    break;
                }
                if (importDTO.getShopId() == null){

                    userFlag = false;
                    msg = "门店为空！";
                    failureNum++;
                    failureMsg.append(failureNum + ". " + importDTO.getName() + msg);
                    break;
                }
                if (importDTO.getCodeNum() == null){

                    userFlag = false;
                    msg = "抽奖码数量为空！";
                    failureNum++;
                    failureMsg.append(failureNum + ". " + importDTO.getName() + msg);
                    break;
                }

                Recharge recharge = new Recharge();

                recharge.setName(importDTO.getName());

                recharge.setActivityId(importDTO.getActivityId());

                recharge.setShopId(importDTO.getShopId());

                recharge.setCodeNum(importDTO.getCodeNum());

                recharge.setMoney(new BigDecimal(importDTO.getCodeNum() * 10));

                recharge.setCreateTime(new Date());

                rechargeMapper.insertRecharge(recharge);

                if (!userFlag) {

                    successNum++;

                }
            } catch (Exception e) {
                failureNum++;
                String msg1 = failureNum + ". 账号 " + importDTO.getName() + " 导入失败：";
                failureMsg.append(msg1 + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }
}
